package com.excesys.exsecs.energy.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.energy.entity.CostHourFlow;
import com.excesys.exsecs.energy.service.CostHourFlowService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 能耗费用小时统计表
 *
 * @author pig
 * @date 2024-10-29 13:12:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/costhourflow")
@Tag(description = "costHourFlow", name = "能耗费用小时统计表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CostHourFlowController {

    private final CostHourFlowService costHourFlowService;

    /**
     * 分页查询
     *
     * @param page            分页对象
     * @param costHourFlow 能耗费用小时统计表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('pig_costHourFlow_view')")
    public R getcostHourFlowPage(@ParameterObject Page page, @ParameterObject CostHourFlow costHourFlow) {
        LambdaQueryWrapper<CostHourFlow> wrapper = Wrappers.lambdaQuery();
        return R.ok(costHourFlowService.page(page, wrapper));
    }


    /**
     * 通过id查询能耗费用小时统计表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('pig_costHourFlow_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(costHourFlowService.getById(id));
    }

    /**
     * 新增能耗费用小时统计表
     *
     * @param costHourFlow 能耗费用小时统计表
     * @return R
     */
    @Operation(summary = "新增能耗费用小时统计表", description = "新增能耗费用小时统计表")
    @SysLog("新增能耗费用小时统计表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pig_costHourFlow_add')")
    public R save(@RequestBody CostHourFlow costHourFlow) {
        return R.ok(costHourFlowService.save(costHourFlow));
    }

    /**
     * 修改能耗费用小时统计表
     *
     * @param costHourFlow 能耗费用小时统计表
     * @return R
     */
    @Operation(summary = "修改能耗费用小时统计表", description = "修改能耗费用小时统计表")
    @SysLog("修改能耗费用小时统计表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pig_costHourFlow_edit')")
    public R updateById(@RequestBody CostHourFlow costHourFlow) {
        return R.ok(costHourFlowService.updateById(costHourFlow));
    }

    /**
     * 通过id删除能耗费用小时统计表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除能耗费用小时统计表", description = "通过id删除能耗费用小时统计表")
    @SysLog("通过id删除能耗费用小时统计表")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('pig_costHourFlow_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(costHourFlowService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param costHourFlow 查询条件
     * @param ids             导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pig_costHourFlow_export')")
    public List<CostHourFlow> export(CostHourFlow costHourFlow, Long[] ids) {
        return costHourFlowService.list(Wrappers.lambdaQuery(costHourFlow).in(ArrayUtil.isNotEmpty(ids), CostHourFlow::getId, ids));
    }
}